package com.ict.web.controller.yzq;


import com.ict.common.core.controller.BaseController;
import com.ict.common.core.domain.AjaxResult;
import com.ict.common.core.page.TableDataInfo;
import com.ict.common.utils.SecurityUtils;
import com.ict.web.msg.domain.SysMsg;
import com.ict.web.msg.dto.QuerySysMsgDto;
import com.ict.web.msg.service.ISysMsgService;
import com.ict.web.msg.util.CreateMsgUtil;
import com.ict.web.msg.vo.SysMsgVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 消息Controller
 *
 * @author xc
 */
@RestController
@RequestMapping("/msg")
@Api(value = "SysMsgController", tags = "消息")
public class SysMsgController extends BaseController
{
    @Autowired
    private ISysMsgService sysMsgService;
    /**
     * 根据登录用户查询通知列表
     */
    @GetMapping(value = "/list")
    @ApiOperation("根据登录用户查询所有未读列表,超级管理员返回所有未读数据(最近的99条消息)")
    public TableDataInfo listByUserId()
    {
        String content="sysDept.getDeptName()+CreateMsgUtil.SUBMITTED+projectPlanName";
        CreateMsgUtil.createMsg(content,null,null,CreateMsgUtil.DECLARATION_SUBMISSION_SUBMITTED_TITLE, SysMsg.RESOURCE_TYPE_DATA,null, null);
        startPage();
        //是否是管理员
        Long userId = SecurityUtils.getUserId();
        QuerySysMsgDto dto=new QuerySysMsgDto();
        if (!SecurityUtils.isAdmin(userId)){
            dto.setReceiveUserId(userId);
        }
        dto.setMsgStatus(SysMsg.STATUS_NORMAL);
        dto.setReadFlag(SysMsg.READ_FLAG_NORMAL);
        List<SysMsgVo> list = sysMsgService.selectSysMsgList(dto);
        return getDataTable(list);
    }
    /**
     * 根据登录用户查询所有未读消息数
     */
    @GetMapping("/count")
    @ApiOperation("根据登录用户查询所有未读消息，超级管理员返回所有未读消息数")
    public AjaxResult countByUserId()
    {
        Long userId = SecurityUtils.getUserId();
        //如果是管理员，查所有
        if (SecurityUtils.isAdmin(userId)){
            userId=null;
        }
        int i = sysMsgService.countByUserId(userId);
        if (i<0){
            return AjaxResult.error();
        }
        if (i>99){
            return AjaxResult.success("99+");
        }
        return AjaxResult.success(i);
    }
    /**
     * 根据通知id修改通知状态为已读
     */
    @GetMapping("/update/{id}")
    @ApiOperation("根据通知id修改通知状态为已读")
    public AjaxResult update(@PathVariable("id") String id)
    {
        return toAjax(sysMsgService.updateReadFlagById(id));
    }
}
